package com.demo.mapper;

import com.demo.entity.Carousel;
import com.demo.entity.Category;
import com.demo.entity.Product;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProductMapper {

    @Select("SELECT product.* ,category.category_name  FROM product \n" +
            "inner join category on product.category_id  =category.category_id ")
    List<Product> findAllProduct();

    @Select("<script>" +
            "SELECT product.* ,category.category_name  FROM product \n" +
            "inner join category on product.category_id  =category.category_id \n" +
            "<if test='categoryId != null and categoryId!=\"\"'>" +
            " where product.category_id =#{categoryId} "+
            "</if>" +
            "</script>")
    List<Product> findProductByCategoryId(Integer categoryId);

    @Select("select * from category ")
    List<Category> findAllCategory();


    @Insert("INSERT INTO product\n" +
            "( product_name, category_id, product_title, " +
            "product_intro, product_picture, product_price, product_selling_price, product_num, product_sales)\n" +
            "VALUES( #{productName}, #{categoryId}, #{productTitle}, " +
            "#{productIntro}, #{productPicture}, #{productPrice}, #{productSellingPrice}, #{productNum}, #{productSales});\n")
    int insertProduct(Product product);

    @Select("select @@identity;")
    int findLastInsertId();


    @Insert("INSERT INTO product_picture\n" +
            "(product_id, product_picture)\n" +
            "VALUES(#{productId}, #{productPicture});")
    int insertProductPicture(int productId,String productPicture);

    @Delete("DELETE FROM product_picture\n" +
            "WHERE product_id=#{productId};")
    int deleteProductPicture(int productId);

    @Select("SELECT * FROM product WHERE product_id=#{productId};\n")
    Product findProductById(int productId);

    @Select("select product_picture from product_picture where product_id =#{productId};")
    List<String> findAllPictureByProductId(int productId);

    @Update("UPDATE product\n" +
            "SET product_name=#{productName},  product_title=#{productTitle},\n" +
            "product_intro=#{productIntro},  product_price=#{productPrice}, " +
            "product_selling_price=#{productSellingPrice}, product_num=#{productNum}, " +
            "product_sales=#{productSales} \n" +
            "WHERE product_id=#{productId};\n")
    int updateProduct(Product product);

    @Delete("DELETE FROM product\n" +
            "WHERE product_id=#{productId};")
    int deleteProductById(int productId);
}
